"""
服务端
"""
from threading import Thread
from socket import *

# 初始化socket对象
s = socket()
s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)  # 端口复用
s.bind(('127.0.0.1', 8000))
s.listen(10)


def run(conn, addr):  # 消息函数
    while True:
        msg = conn.recv(1024).decode('utf8')
        if not msg: break
        print(f'收到{addr[1]}的消息：{msg}')
        re_msg = ''.join(['服务器回复:', msg.upper()])
        conn.send(re_msg.encode('utf8'))
    conn.close()


if __name__ == '__main__':
    while True:
        conn, addr = s.accept()  # 循环接收TCP连接
        Thread(target=run, args=(conn, addr)).start()  # 创建线程并启动，将conn连接传入run函数
